package com.sso.managedBean;

import java.util.LinkedList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.system.managedBean.DepartmentListOfOnePersonBean;
import com.sso.entity.DicHuman;
import com.sso.service.IDicHumanService;

@ManagedBean(name = "sso_listHumanOfOneSystemBean", scope = ManagedBeanScope.REQUEST)
public class ListHumanOfOneSystemBean {
	protected IDicHumanService dicHumanService = SpringBeanHolder
			.getBean("dicHumanService");

	private final static Log logger = LogFactory
			.getLog(DepartmentListOfOnePersonBean.class);

	@DataModel(id = "humanGrid")
	private List<DicHuman> data = new LinkedList<DicHuman>();

	@Accessible
	@SaveState
	private String systemId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("systemId");

	@Bind
	@Accessible
	@SaveState
	protected DicHuman system;

	@Inject
	private FacesContext context;

	@Bind(id = "listHumanGrid", attribute = "binding")
	private UIDataGrid listHumanGrid_binding;

	@DataModel(id = "listHumanGrid")
	private List<DicHuman> getHumanGrid() {
		return dicHumanService.getBySystemID(systemId);
	}

	@Action(id = "createHuman")
	public void createHuman() {
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/sso/addHuman.faces?systemId=" + systemId;
		String script = "changeSrc('" + url + "','分配所属用户')";
		Browser.execClientScript(script);
	}

	@Action(id = "editHuman")
	public void editHuman() {

		if (listHumanGrid_binding.getSelectedValues().length > 0) {
			DicHuman human = (DicHuman) listHumanGrid_binding
					.getSelectedValues()[0];
			String url = context.getExternalContext().getRequestContextPath()
					+ "/modules/sso/addHuman.faces?humanId=" + human.getId();
			String script = "changeSrc('" + url + "','修改所属用户')";
			Browser.execClientScript(script);
		}
	}

	@Action(id = "deleteHuman")
	public void deleteHuman() {
		if (listHumanGrid_binding.getSelectedValues().length > 0) {
			DicHuman human = (DicHuman) listHumanGrid_binding
					.getSelectedValues()[0];
			dicHumanService.deleteDicHuman(human);
			data.remove(human);
			listHumanGrid_binding.reload();
		}
	}
}
